package com.wdb.common.util;

import com.google.common.collect.Maps;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

public abstract class GsonUtils {
    private static final Gson GSON;

    static {
        GSON = new GsonBuilder()
                .setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
                }.getType(), new MapDeserializer())
                .registerTypeAdapter(Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG)
                .registerTypeAdapter(Date.class, new DateDeserializer()).setDateFormat(DateFormat.LONG)
                .create();
    }

    /**
     * 获取GSON解析器
     */
    public static Gson get() {
        return GSON;
    }

    /**
     * 对象转JSON
     */
    public static String toJson(Object object) {
        return GSON.toJson(object);
    }

    /**
     * JSON转JsonObject
     */
    public static JsonObject toJsonObject(String json) {
        return new JsonParser().parse(json).getAsJsonObject();
    }

    /**
     * JSON转对象 字节码
     */
    public static <T> T fromJson(String json, Class<T> clz) {
        return GSON.fromJson(json, clz);
    }

    /**
     * JSON转对象 类型
     */
    public static <T> T fromJson(String json, Type type) {
        return GSON.fromJson(json, type);
    }

    /**
     * JsonObject转对象
     */
    public static <T> T fromJsonElement(JsonElement e, Type type) {
        return GSON.fromJson(e, type);
    }

    /**
     * JsonObject转对象
     */

    public static <T> T fromJsonElement(JsonElement e, Class<T> clz) {
        return GSON.fromJson(e, clz);
    }

    /**
     * 时间戳转DATE
     */
    static class DateDeserializer implements JsonDeserializer<Date> {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    }

    /**
     * DATE转时间戳
     */
    static class DateSerializer implements JsonSerializer<Date> {
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.getTime());
        }
    }

    /**
     * MAP反序列化
     */
    public static class MapDeserializer implements JsonDeserializer {
        @Override
        public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            Map<String, Object> map = Maps.newHashMap();

            JsonObject jsonObject = json.getAsJsonObject();

            Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();

            for (Map.Entry<String, JsonElement> entry : entrySet) {
                map.put(entry.getKey(), entry.getValue());
            }

            return map;
        }
    }
}
